package fr.alesia.deepstack.io.impl;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import fr.alesia.deepstack.bean.Ranking;
import fr.alesia.deepstack.data.RankingDao;
import fr.alesia.deepstack.factory.DaoFactory;
import fr.alesia.deepstack.factory.IOFactory;
import fr.alesia.deepstack.io.BeanLoader;
import fr.alesia.deepstack.io.BeanReader;

public class RankingLoader implements BeanLoader<Ranking> {

	private BeanReader<Ranking> reader;
	private static BeanLoader<Ranking> instance = null;
	
	private RankingLoader(){
		
		this.reader = IOFactory.getRankingReader();
	}
	
	public static BeanLoader<Ranking> getInstance(){
		if(instance == null){
			instance = new RankingLoader();
		}
		return instance;
	}
	
	public void load(InputStream stream) throws Exception {
		
		RankingDao rankingDao = DaoFactory.getRankingDao();
		
		Reader reader = new InputStreamReader(stream);
		BufferedReader buffer = new BufferedReader(reader);
		
		String line = null;
		
		while((line = buffer.readLine()) != null){
			
			Ranking ranking = this.reader.read(line);
			rankingDao.insert(ranking);
		}
		
		buffer.close();
		reader.close();
		
		buffer = null;
		reader = null;
	}
}
